﻿define(function(require,exports,module) {
    var myPub = require('./public');

    var myPubbaseurl = baseurl;
    //评论星级
    var oEstimateStars = $('.js_estimateStars');	                        //评价区域
    var oEstimateStars_Lv = $('.js_estimateStars_Lv');                      //评价等级信息展示
    var oNoNameCheck = $('.js_noNameCheck'); 		                        // 匿名选择
    var oEvaluationPrize = $('.js_evaluationPrize');                        // 邀请有礼规则入口
    var oLookForRuler = $('.js_lookForRuler');                              // 查看规则
    var oDDJPhotoLoad = $(".js_ddjPhotoLoad");                              // 上传图片功能
    var rulerCon = [];                                                      // 评价有礼规则内容
    var rulerHtml = '';                                                     // 评价有礼规则内容转换成标签
    var commentRulerUrl = '/m/g/comment/1.0/getCommentRule';                // 获取评论规则

    var oBtnConfirm = $('.js_btnConfirm');

    //评价等级信息
    var oEstimateStars_LvInfo = ['非常差','差','一般','很好','非常好'];

    var maxNum = 4;

    // 获取评价有礼规则
    getRulerData();
    function getRulerData(){
        G_JSON_GET(commentRulerUrl,{},function (data) {
            var dataItem = data.data;
            if(data.ret.code == 0){
                // 处理活动规则的数据
                var thtml = '';
                if(dataItem && dataItem.indexOf('\n') != -1){
                    rulerCon = dataItem.split('\n')
                }else if(dataItem && dataItem.length>0){
                    rulerCon = [dataItem];
                }
                if(rulerCon && rulerCon.length>0){
                    oEvaluationPrize.show();
                    for(var i = 0;i<rulerCon.length;i++){
                        thtml+='<p>'+rulerCon[i]+'</p>'
                    }
                    rulerHtml = '<div class="rulerPrizeComment">'+thtml+'</div>'
                }
            }
        })
    }
    // 查看评价有礼的规则
    oLookForRuler.on('click',function () {
        myPub.TipLayerLongClose('规则',rulerHtml,'no',function () {
            $('.layermchild').addClass('commentRuler')
        })
    });

    //评价星星设置
    oEstimateStars.each(function (i) {

        // 初始化星星状态和文字信息
        oEstimateStars.eq(i).find('li').addClass('active');
        oEstimateStars_Lv.eq(i).text(oEstimateStars_LvInfo[maxNum])

        // 添加点击事件
        oEstimateStars.eq(i).find('li').hammer().on('tap',function() {
            var index = $(this).index();
            var pIndex = oEstimateStars.index($(this).parents('.js_estimateStars'));
            var _this = $(this);

            if(_this.hasClass('active')) {
                _this.removeClass('active');

                for(var n=maxNum; n > index; n--) {
                    oEstimateStars.eq(pIndex).find('li').eq(n).removeClass('active')
                }
                if(index == 0) {
                    oEstimateStars.eq(pIndex).find('li').eq(index).addClass('active');
                    //评价信息
                    oEstimateStars_Lv.eq(pIndex).text(oEstimateStars_LvInfo[index])
                }
                else {
                    oEstimateStars_Lv.eq(pIndex).text(oEstimateStars_LvInfo[index-1])
                }
            }
            else {
                _this.addClass('active');
                if(index > 0) {
                    for(var i=0; i < index; i++) {
                        oEstimateStars.eq(pIndex).find('li').eq(i).addClass('active')
                    }
                }
                //评价信息
                oEstimateStars_Lv.eq(pIndex).text(oEstimateStars_LvInfo[index])
            }

        })
    });


    // 匿名选择
    oNoNameCheck.hammer().on('tap', function () {
        if(oNoNameCheck.find('.js_checkBox').hasClass('active')) {
            oNoNameCheck.find('.js_checkBox').removeClass('active')
        }
        else {
            oNoNameCheck.find('.js_checkBox').addClass('active')
        }
    });

    oBtnConfirm.hammer().on('tap', function () {
        //移除监听，避免重复提交
        oBtnConfirm.hammer().off('tap');
        submitForm();
    })

    function submitForm() {
        var discription = $("#comment").val();


        if(discription.trim() == "" || discription.trim() == null){
            myPub.TipLayer("请填写评价内容！",'no',function () {
                // myPub.LayerCloseAll();
            });
            oBtnConfirm.hammer().on('tap', function () {
                oBtnConfirm.hammer().off('tap');
                submitForm();
            })

            return;
        }
        //去除标点符号之后，字数必须大于5
        discription=discription.replace(/[\ |\~|\`|\!|\@|\#|\$|\%|\^|\&|\*|\(|\)|\-|\_|\+|\=|\||\\|\[|\]|\{|\}|\；|\：|\《|\》|\，|\。|\？|\！|\“|\”|\;|\:|\"|\'|\,|\<|\.|\>|\/|\?]/g,"");
        if(discription.length < 5){
            myPub.TipLayer("评论内容至少5个字（标点符号不计算在内）",'no',function () {
            });
            oBtnConfirm.hammer().on('tap', function () {
                oBtnConfirm.hammer().off('tap');
                submitForm();
            })

            return;
        }
        if($('.js_noNameCheck').find('.js_checkBox').hasClass('active')) {
            $('#noName').val('true');
        }
        else {
            $('#noName').val('false');
        }
        $("#serviceStar").val($('#service_star_div').find("li.active").length);
        $("#commentstar").val($('#star_div').find("li.active").length);

        // var url = "/shop/goodsComment/saveComment";
        var url = "/m/g/comment/1.0/saveGoodsComment";
        if($('.js_noNameCheck').find('.js_checkBox').length == 0){
            //追评
        	// url = "/shop/goodsComment/saveAppendComment";
        	url = "/m/g/comment/1.0/saveGoodsCommentAppend";
        }

        /*
        * type
        * 0无图  1有图  2有视频 3有图有视频
        * */
        var dataStr = '';
        if(oDDJPhotoLoad.find('.js_uploadImage').length > 0){
            dataStr = $("#commentform").serialize()+'&type=1'
        }else{
            dataStr = $("#commentform").serialize()+'&type=0'
        }
        //收集图片资源
        G_JSON_POST(myPubbaseurl+url,dataStr,function(data){
            var _productId = dg_orderDetailInfo[0].productId;
            var message = '';
            oBtnConfirm.hammer().on('tap', function () {
                oBtnConfirm.hammer().off('tap');
                submitForm();
            });

            // 用户数据提交
            // userAction(oBtnConfirm, _productId);
            if(data.ret.code == 0){
                message = data.data
            }else{
                message = data.ret.message
            }
            myPub.TipBtnLayer(message,'no',null,function () {
                window.location.href = '/shop/pay/order_list';
            });

            // if(data.status == 'success'){
            //     myPub.TipBtnLayer(data.message,'no',null,function () {
            //         window.location.href = '/shop/pay/order_list';
            //     });
            // }else{
            //     myPub.TipBtnLayer(data.message,'no',null,function () {
            //         window.location.href = '/shop/pay/order_list';
            //     });
            // }
        });
    }
    // 添加用户行为
    function userAction(obj, keyword) {
        ddjhoc.setData(obj, 'comment', 'add', function () {
            console.log('params.comment', ddjhoc.params);
            ddjhoc.sendData( ddjhoc.params );
        }, keyword);
    }


});



